﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Ionic.Zip;

namespace PackToKindle
{
    class Zipper : IBookStorage
    {
        public IEnumerable<string> Zip()
        {
            var zipFileName = _GetTempZipFileName();

            using (ZipFile zip = new ZipFile())
            {
                zip.AlternateEncoding = Encoding.UTF8;
                zip.UseZip64WhenSaving = Zip64Option.AsNecessary;
                zip.AlternateEncodingUsage = ZipOption.AsNecessary;

                foreach(var filePath in _filesToZip)
                    zip.AddFile(filePath);

                zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G");

                zip.Save(zipFileName);
            }

            _filesToZip.Clear();
            CurrentSize = 0;

            return new string[] { zipFileName };
        }

        public void AddFile(FileInfo file)
        {
            CurrentSize += file.Length;
            _filesToZip.Add(file.FullName);
        }

        public long CurrentSize { get; private set; }

        #region Private

        private string _GetTempZipFileName()
        {
            return System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".zip";
        }

        private List<string> _filesToZip = new List<string>(); 

        #endregion
    }
}
